package cn.xfdzx.android.apps.shop.activity;

import android.content.res.ColorStateList;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.HomeVPAdapter;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.evenbus.UpdateMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateMyOrderMessage;
import cn.xfdzx.android.apps.shop.fragment.AllOrderFragment;
import cn.xfdzx.android.apps.shop.fragment.OneFragment;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

/**
 * 我的订单页面
 */
public class AllOrderActivity extends BaseMvpActivity {

    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.tl_fragment_all_order)
    TabLayout mTabLayout;
    @BindView(R.id.vp_fragment_all_order)
    ViewPager vpFragmentAllOrder;
    @BindView(R.id.iv_location_back)
    ImageView iv_location_back;

    private RelativeLayout.LayoutParams item_bar;
    private List<Fragment> mFragments;
    private String[] title = {"全部", "待支付", "待收货", "待评价" };
    private HomeVPAdapter homeVPAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_all_order;
    }

    @Override
    public void initView() {
        initHigh();
        mFragments = new ArrayList<>();
        mFragments.add(AllOrderFragment.newInstance("all"));//
        mFragments.add(AllOrderFragment.newInstance("0"));//
//        mFragments.add(AllOrderFragment.newInstance("1"));//
        mFragments.add(AllOrderFragment.newInstance("2"));//
        mFragments.add(AllOrderFragment.newInstance("3"));//
//        mFragments.add(AllOrderFragment.newInstance("-3"));//

        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(this.getResources().getColor(R.color.picture_color_transparent)));
        for (String tab : title) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tab));
        }
        homeVPAdapter = new HomeVPAdapter(getSupportFragmentManager(), title, mFragments);
        vpFragmentAllOrder.setAdapter(homeVPAdapter);
        mTabLayout.setupWithViewPager(vpFragmentAllOrder);

        if (null != getIntent().getStringExtra("from")) {
            if (getIntent().getStringExtra("from").equals("pay_successful")) {
                vpFragmentAllOrder.setCurrentItem(2);
            } else if (getIntent().getStringExtra("from").equals("pay_cancel")) {
                vpFragmentAllOrder.setCurrentItem(1);
            } else if (getIntent().getStringExtra("from").equals("my_center")) {
                vpFragmentAllOrder.setCurrentItem(getIntent().getIntExtra("position", 0));
            }
        }
        if ( TYPE_IS_RETAIL == 0) {
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_FB9129), getResources().getColor(R.color.color_FB9129));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_FB9129));
        } else {
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_02C557), getResources().getColor(R.color.color_02C557));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_02C557));
        }

        vpFragmentAllOrder.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    EventBus.getDefault().post(new UpdateMyOrderMessage("all"));
                } else if (position == 1) {
                    EventBus.getDefault().post(new UpdateMyOrderMessage("0"));
                } else if (position == 2) {
                    EventBus.getDefault().post(new UpdateMyOrderMessage("2"));
                } else if (position == 3) {
                    EventBus.getDefault().post(new UpdateMyOrderMessage("3"));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initOnPreDrawListener();

        iv_location_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void initOnPreDrawListener() {
        final ViewTreeObserver viewTreeObserver = this.getWindow().getDecorView().getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (vpFragmentAllOrder.getCurrentItem() == 0) {
                    EventBus.getDefault().post(new UpdateMyOrderMessage("all"));
                } else if (vpFragmentAllOrder.getCurrentItem() == 1) {
                    EventBus.getDefault().post(new UpdateMyOrderMessage("0"));
                } else if (vpFragmentAllOrder.getCurrentItem() == 2) {
                    EventBus.getDefault().post(new UpdateMyOrderMessage("2"));
                } else if (vpFragmentAllOrder.getCurrentItem() == 3) {
                    EventBus.getDefault().post(new UpdateMyOrderMessage("3"));
                }
                // 移除OnPreDrawListener事件监听
                getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);

                return true;
            }
        });
    }
    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
